package com.dinstar.dinlink;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.Nullable;

import com.dinstar.dinlink.activity.LoginActivity;
import com.dinstar.dinlink.broadcast.MyMixPushReceiver;
import com.dinstar.dinlink.core.VFS;
import com.dinstar.dinlink.utils.Cacher;
import com.dinstar.linphone.Preferences;
import com.mixpush.core.GetRegisterIdCallback;
import com.mixpush.core.MixPushClient;
import com.mixpush.core.MixPushLogger;
import com.mixpush.core.MixPushPlatform;

import org.linphone.core.Factory;

import core.library.com.widget.toupload.update.UpdateAppUtils;

public class Dinlink extends Application {
    private static Dinlink app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        //设置app crash后自动重启，后续可以设置释放资源
        Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
            restartApp();
        });

        Context ctx = super.getApplicationContext();
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Dinlink:AppCreation");
        wakeLock.acquire(20*1000L);

        Factory factory = Factory.instance();
        factory.enableLogcatLogs(true);
        factory.setLogCollectionPath(ctx.getFilesDir().getAbsolutePath());
//        factory.setCacheDir(ctx.getCacheDir().getAbsolutePath());
        if (VFS.isEnabled(ctx)) {
            VFS.setup(ctx);
        }
        Cacher.init(this);

        UpdateAppUtils.init(this);

        MixPushClient.getInstance().setLogger(new MixPushLogger() {
            @Override
            public void log(String tag, String content, Throwable throwable) {
                Log.e(tag, content);
                throwable.printStackTrace();
            }

            @Override
            public void log(String tag, String content) {
                Log.e(tag, content);
            }
        });
        MixPushClient.getInstance().setPushReceiver(new MyMixPushReceiver());
        MixPushClient.getInstance().register(this);
        MixPushClient.getInstance().getRegisterId(this, new GetRegisterIdCallback() {
            @Override
            public void callback(@Nullable MixPushPlatform platform) {
                Log.e("GetRegisterIdCallback", "notification $platform");
            }
//            override fun callback(platform:MixPushPlatform?) {
//                Log.e("GetRegisterIdCallback", "notification $platform")
//            }
        });

    }


    private void restartApp() {
        Intent i = new Intent(app, LoginActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        app.startActivity(i);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }
}
